package com.study.streamApi;

import org.junit.Test;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 时间
 *
 * @author yangyanbin
 * @since 20221028
 **/
public class DateStream {
    @Test
    public void test1() {
        List<Date> list = new ArrayList<>();


        list.add(parseDate("2022/10/27 O8:09:00"));
        list.add(parseDate("2022-10-26 11:09:00"));
        list.add(parseDate("2022-10-28 13:09:00"));
        list.add(parseDate("2022-10-28 11:09:00"));
        list.add(parseDate("2022-10-28 18:09:00"));
        list.add(parseDate("2022-10-29 18:09:00"));
        list.add(parseDate("2022-10-29 O8:09:00"));
        Date date1 = new Date();

        List<Date> dateList =
                list.stream().filter(x -> x.compareTo(getStartOfDay(date1)) >= 0 && x.compareTo(getEndOfDay(date1)) <= 0).collect(Collectors.toList());

        System.out.println("dataList" + dateList);

    /*    List<Date> dateList4 =
                list.stream().filter(x -> x.toInstant().isAfter(getStartOfDay(date1).toInstant())
                        ||  x.toInstant().isAfter(getEndOfDay(date1).toInstant())).collect(Collectors.toList());*/


        /*  System.out.println("djcisodjc:"+dateList4.size());*/

        List<Date> dates = list.stream().filter(x -> x.before(getEndOfDay(new Date()))
                && x.after(getStartOfDay(new Date()))).collect(Collectors.toList());

        System.out.println("dates:" + dates);


        List<Date> list2 = new ArrayList<>();
        for (Date date3 : list) {
            if (date3.compareTo(getStartOfDay(date1)) >= 0 && date3.compareTo(getEndOfDay(date1)) <= 0) {
                list2.add(date3);
            }

        }
        System.out.println("oooo" + list2.size());
        for (Date date : list2) {
            System.out.println(date);
        }
        System.out.println("xxxxxxxxxxxxxxxx");
/*        System.out.println(dateList.size());
        for (Date date : dateList) {
            System.out.println(date);
        }*/

        System.out.println("+++++++++++");
        System.out.println(list.get(2));
        System.out.println(list.get(2).compareTo(getStartOfDay(new Date())));
        System.out.println(list.get(2).compareTo(getEndOfDay(new Date())));

    }


    public static Date getEndOfDay(Date date) {
        LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(date.getTime()), ZoneId.systemDefault());
        LocalDateTime endOfDay = localDateTime.with(LocalTime.MAX);
        return Date.from(endOfDay.atZone(ZoneId.systemDefault()).toInstant());
    }

    /**
     * @Description: 获得某天最小时间 2022-02-17 00:00:00
     * @Param: [date]
     * @return:
     */
    public static Date getStartOfDay(Date date) {
        LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(date.getTime()), ZoneId.systemDefault());
        LocalDateTime startOfDay = localDateTime.with(LocalTime.MIN);
        return Date.from(startOfDay.atZone(ZoneId.systemDefault()).toInstant());
    }


    public static Date transferString2Date(String s) {
        Date date = null;
        try {
            date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(s);
        } catch (ParseException e) {
            //LOGGER.error("时间转换错误, string = {}", s, e);
        }
        return date;
    }


    public static Date parseDate(String strDate) {
        Date date = null;
        if (Objects.nonNull(strDate)) {
            try {
                SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                date = df.parse(strDate);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
        return date;
    }

}
